@extends('admin.layout.layout', ['row' => @$page])

@section('contain')


    <div class="portlet">
        <div class="portlet-title">
            <div class="caption">
                <x-portlet-breadcrumb :page="$page"></x-portlet-breadcrumb>
            </div>
            <div class="actions">
                <div class="item">
                    <a href="/{{ request()->path() }}/download_template" class="btn btn-primary">{{ ts('DOWNLOADTEMPLATE') }}</a>
                </div>
                <div class="item">
                    <a href="/{{ request()->path() }}/bulk_import" class="btn btn-primary">{{ ts('BULKIMPORT') }}</a>
                </div>
                @if(cWPC())
                    <div class="item">
                        <a href="/{{ request()->path() }}/create" class="btn btn-primary">{{ ts('addnew') }}</a>
                    </div>
                @endif
            </div>
        </div>

        <div class="filter-wrapper">
            <form action="/{{ request()->path() }}/" class="form-inline filter-form">
                <div class=" input-group mb-3 custom-control-inline">
                    <input type="text" class="form-control s-input" name="value" value="{{ request()->input('value') }}" placeholder="{{ ts('WARRANTYSEARCHTEXT') }}">
                </div>

                <div class="input-group mb-3 custom-control-inline">
                    <select name="status" class="form-control">
                        <option value="">{{ ts('select').' '.ts('status') }}</option>
                        <option value="0" {{ request()->input('status') !== null && request()->input('status') == 0 ? 'selected' : '' }}>{{ ts('STA0') }}</option>
                        <option value="1" {{ request()->input('status') == 1 ? 'selected' : '' }}>{{ ts('STA1') }}</option>
                        <option value="2" {{ request()->input('status') == 2 ? 'selected' : '' }}>{{ ts('STA2') }}</option>
                        <option value="3" {{ request()->input('status') == 3 ? 'selected' : '' }}>{{ ts('STA3') }}</option>
                    </select>
                </div>
                <x-flatpickr-date-range></x-flatpickr-date-range>
                <div class="mb-3">
                    <button class="btn btn-primary">{{ ts('SEARCH') }}</button>
                    <a href="/{{ request()->path() }}/" class="btn btn-warning">{{ ts('RESET') }}</a>
                </div>

            </form>
        </div>

        <div>
            <table class="table text-nowrap bt-table d-none">
                <thead>
                <tr class="order_column">
                    <th style="width: 50px;"></th>
                    <th style="width: 75px;">{{ ts('sequence') }}</th>
                    <th data-field="warranty_id" class="sortable {{ setOrderClass('warranty_id') }}">{{ ts('warrantyid') }}</th>
                    <th data-field="roll_number" class="sortable {{ setOrderClass('roll_number') }}">{{ ts('ROLLID') }}</th>
                    <th data-field="first_name" class="sortable {{ setOrderClass('first_name') }}" >{{ ts('customername') }}</th>
                    <th class="sortable {{ setOrderClass('license_plate') }}" data-field="license_plate">{{ ts('licenseplate') }}</th>
                    <th data-field="wvclen" class=" {{ setOrderClass('wvclen') }}" >{{ ts('ylcd2') }}</th>
                    <th class="sortable {{ setOrderClass('installation_date') }}" data-field="installation_date">{{ ts('installationdate') }}</th>
                    <th class="sortable {{ setOrderClass('installer_name') }}" data-field="installer_name">{{ ts('INSTALLERNAME') }}</th>
                    <th class="sortable {{ setOrderClass('abbr') }}" data-field="abbr">{{ ts('qudao') }}</th>
                    <th class="sortable {{ setOrderClass('approved') }}" data-field="approved" style="width: 100px;">{{ ts('status') }}</th>
                    <th style="width:270px">{{ ts('OPERATIONS') }}</th>
                </tr>
                </thead>
                <tbody>
                @foreach($data as $k => $v)
                    @if(session('hightlight') && session('hightlight.type')=='warranty' && session('hightlight.id')==$v->id)
                        <tr class="item-list table-warning">
                    @else
                        <tr class="item-list">
                            @endif
                        <td class="text-center">
                            <input type="checkbox" class="warranty_icheck icheck_input" name="ids[]" value="{{ $v->id }}">
                        </td>
                        <td class="text-center">{{ @$data->firstItem()+$k }}</td>
                        <td>
                            <div class="truncated-3">{{ @$v->warranty_id }}</div>
                        </td>
                        <td>
                            @foreach(@$v->warranty_to_vehicle_coverage as $r)
                                <div>{{ @$r->roll_number }}</div>
                            @endforeach
                        </td>
                        <td>
                            <div class="truncated-3">{{ @$v->first_name }}</div>
                        </td>
                        <td>
                            <div class="truncated-3">{{ @$v->license_plate }}</div>
                        </td>
                        <td>
                            {{--<div class="truncated-3">{{ array_sum(\Illuminate\Support\Arr::pluck(@$v->warranty_to_vehicle_coverage, 'length')) }} cm</div>--}}
                            <div>{{ @$v->wvclen ?: 0 }} cm</div>
                        </td>
                        <td>
                            {{ fdate(@$v->installation_date) }}
                        </td>
                        <td>
                            <div class="truncated-3">{{ @$v->installer_name }}</div>
                        </td>
                        <td>
                            <div class="truncated-3">{{ @$v->abbr }}</div>
                            {{--<div class="truncated-3">{{ @$v->dealer->company_name }}</div>--}}
                        </td>
                        <td>
                            <div class="truncated-3">
                                {{ ts('sta'.$v->approved) }}
                            </div>
                        </td>
                        <td>
                            @if(session('admin.user_type') == 1)
                                @if($v->approved==1)
                                    <button type="button" onclick="smsModal('{{ $v->id }}')" class="btn btn-warning px-2 btn-sm" style="padding-top: 3px;padding-bottom: 3px;">
                                        <img src="/images/phone_sms.png" style="height: 19px;display: inline-block;">
                                    </button>
                                @else
                                    <button type="button" disabled class="btn btn-secondary px-2 btn-sm" style="padding-top: 3px;padding-bottom: 3px;">
                                        <img src="/images/phone_sms.png" style="height: 19px;display: inline-block;">
                                    </button>
                                @endif
                            @endif
                            <a class="btn btn-success btn-sm" href='/{{ request()->path() }}/{{ $v->id }}'>{{ ts('VIEW') }}</a>
                            @if(session('admin.user_type')==1)
                                @if(@$v->approved != 1)
                                    <a class="btn btn-warning btn-sm" href="/{{ request()->path() }}/{{ $v->id }}/edit">{{ ts('xg') }}</a>
                                @else
                                    <button class="btn btn-secondary btn-sm" disabled>{{ ts('edit') }}</button>
                                @endif
                                <span class="btn btn-danger btn-sm" onclick="handleDelete3('{{ $v->id }}', '质保 {{ @$v->warranty_id }}')">{{ ts('delete') }}</span>
                                    {{--@if($v->approved==3)
                                        <span class="btn btn-primary btn-sm" onclick="handleWFQSH('{{ $v->id }}', '{{ @$v->warranty_id }}')">{{ ts('FQSH') }}</span>
                                    @endif--}}
                            @else
                                @if($v->approved==3)
                                    <a class="btn btn-warning btn-sm" href="/{{ request()->path() }}/{{ $v->id }}/edit">{{ ts('xg') }}</a>
                                    <span class="btn btn-danger btn-sm" onclick="handleDelete3('{{ $v->id }}', '质保 {{ @$v->warranty_id }}')">{{ ts('delete') }}</span>
                                    {{--<span class="btn btn-primary btn-sm" onclick="handleWFQSH('{{ $v->id }}', '{{ @$v->warranty_id }}')">{{ ts('FQSH') }}</span>--}}
                                @elseif($v->approved==2)
                                    <a class="btn btn-warning btn-sm" href="/{{ request()->path() }}/{{ $v->id }}/edit">{{ ts('edit') }}</a>
                                    <span class="btn btn-danger btn-sm" onclick="handleDelete3('{{ $v->id }}', '质保 {{ @$v->warranty_id }}')">{{ ts('delete') }}</span>
                                @endif
                            @endif
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>

            <div class="row my-3">
                <div class="col-auto">
                    <label style="cursor: pointer"><input type="checkbox" class="icheck_all" data-ele=".warranty_icheck"><span class="ml-2">{{ ts('SELECTALL') }}</span></label>
                    <span class="btn btn-primary ml-3" onclick="deleteAlliCheck('.warranty_icheck', '/admin/warranty/deletes')">{{ ts('delete') }}</span>
                </div>
                <div class="col">
                    {{ $data->appends(request()->input())->links() }}
                </div>
            </div>
        </div>
        <style>
            #smsModal .modal-body{
                padding: 15px;
            }
            #smsModal .row{
                margin-bottom: 20px;
            }
            #smsModal .row .col-6:first-child{
                padding-left: 50px;
            }
            #smsModal .modal-footer{
                justify-content: center;
            }
            #smsModal .modal-footer .btn +.btn{
                margin-left: 2rem;
            }
        </style>

        <form class="modal fade modal-s1" id="smsModal">
            <input type="text" hidden name="warranty_id">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title font-green">{{ ts('RESENDSMSAGAIN') }}</h5>
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-6">{{ ts('CUSTOMERNAME') }}</div>
                            <div class="col-6" id="CUSTOMERNAME"></div>
                        </div>
                        <div class="row">
                            <div class="col-6">{{ ts('PHONENUMBER') }}</div>
                            <div class="col-6" id="PHONENUMBER"></div>
                        </div>
                        <div class="row">
                            <div class="col-6">{{ ts('YEAR') }}</div>
                            <div class="col-6" id="YEAR"></div>
                        </div>
                        <div class="row">
                            <div class="col-6">{{ ts('make') }}</div>
                            <div class="col-6" id="MAKE"></div>
                        </div>
                        <div class="row">
                            <div class="col-6">{{ ts('MODEL') }}</div>
                            <div class="col-6" id="MODEL"></div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-primary" onclick="sendSMS()">{{ ts('confirm') }}</button>
                        <button type="button" class="btn btn-danger" data-dismiss="modal">{{ ts('cancel') }}</button>
                    </div>
                </div>
            </div>
        </form>
    </div>


@endsection

@section('script')
    <script>
        $('.table.bt-table').bootstrapTable({
            fixedColumns: true,
            fixedNumber: 1,
            fixedRightNumber: 1,
        }).removeClass('d-none')
        
        function smsModal(id) {
            $('#smsModal').find('[name="warranty_id"]').val(id)
            request.get('/admin/ajax/getWSF', {
                params: {
                    id: id
                }
            })
                .then(function (d) {
                    var doc = d.data;
                    $('#smsModal').find('#CUSTOMERNAME').text(doc.customer_name);
                    $('#smsModal').find('#PHONENUMBER').text(doc.phone_number);
                    $('#smsModal').find('#YEAR').text(doc.year)
                    $('#smsModal').find('#MAKE').text(doc.make)
                    $('#smsModal').find('#MODEL').text(doc.model)
                    $('#smsModal').modal();
                })
                .catch(function (d) {

                })
        }

        function sendSMS() {
            var id = $('#smsModal').find('[name="warranty_id"]').val()
            request.post('/admin/ajax/sendSMS', {
                id: id
            })
                .then(function (d) {
                    var data = d.data;
                    sw3(d.msg || '发送成功')
                })
                .catch(function (d) {
                    var data = d.data;
                    sw3(data.msg || '发送失败', 'error')
                })

            console.log(id)
        }
    </script>
@endsection